const {characterLengthLimit,post}=require('../config/config');

module.exports=data=>{
    let {title,tags,intro,content}=data,res=(msg,verify=false)=>({verify,msg});

    if(!title)return res('标题未输入');
    title=title.trim();
    if(title.length<characterLengthLimit.post.title[0] || title.length>characterLengthLimit.post.title[1])return res(`标题长度非法(${characterLengthLimit.post.title[0]}~${characterLengthLimit.post.title[1]})`);

    if(!tags || !tags.length)return res('标签未选择');
    if(tags){
        if(!Array.isArray(tags))tags=Array(tags);
        if(!tags.every(tag=>post.tags.includes(tag)))return res('标签选择非法');
    }
    //去重
    tags=new Set(tags);
    tags=Array.from(tags);

    if(!intro)return res('简介未输入');
    intro=intro.trim();
    if(intro.length<characterLengthLimit.post.intro[0] || intro.length>characterLengthLimit.post.intro[1])return res(`简介内容长度非法(${characterLengthLimit.post.intro[0]}~${characterLengthLimit.post.intro[1]})`);

    if(!content)return res('正文未输入');
    content=content.trim();
    if(content.length<characterLengthLimit.post.content[0] || content.length>characterLengthLimit.post.content[1])return res(`正文内容长度非法(${characterLengthLimit.post.content[0]}~${characterLengthLimit.post.content[1]})`);

    return {
        verify:true,
        post:{
            title,
            tags,
            intro,
            content
        }
    };
};